package by.alex.dzeshko.lastfmsimple.app.activity;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import by.alex.dzeshko.lastfmsimple.R;
import by.alex.dzeshko.lastfmsimple.http.HttpManager;
import by.alex.dzeshko.lastfmsimple.utils.Md5;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class AuthActivity extends SherlockFragmentActivity {

	protected static final String LOG_TAG = "AuthActivity";

	EditText mEditTextUsername, mEditTextPassword;

	Button mButtonNext, mButtonCancel;

	ProgressBar mProgressBar;

	private String API_KEY = "2dba0b63309d933a739fe400767562b1",
			SECRET = "is 51889cf0bc6a399ea55168dc9fefe414";

	@Override
	protected void onCreate(Bundle savedState) {
		super.onCreate(savedState);
		setContentView(R.layout.activity_auth);
		mEditTextUsername = (EditText) findViewById(R.id.edittext_auth_username);
		mEditTextPassword = (EditText) findViewById(R.id.edittext_auth_password);
		mButtonCancel = (Button) findViewById(R.id.button_auth_cancel);
		mButtonNext = (Button) findViewById(R.id.button_auth_next);
		mProgressBar = (ProgressBar) findViewById(R.id.progressbar_auth);
		mProgressBar.setVisibility(View.INVISIBLE);
		mButtonCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent cancel = new Intent(AuthActivity.this,
						MainActivity.class);
				cancel.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
				startActivity(cancel);
			}
		});
		mButtonNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String username = mEditTextUsername.getText().toString();
				String password = mEditTextPassword.getText().toString();
				StringBuilder builder = new StringBuilder("api_key");
				String token = Md5.convert("alexdzeshko"+Md5.convert("hujykoli"));
				String template = "api_key"
						+ API_KEY
						+ "authToken" + token
						+ "methodauth.getMobileSessionpasswordhujykoliusernamealexdzeshko"
						+ SECRET;
				// builder.append(API_KEY).append("methodauth.getMobileSessionpassword")
				// .append("hujykoli").append("username").append("alexdzeshko").append(SECRET);
				Log.d(LOG_TAG, template);
				String presign = Md5.convert(template);
				Log.d(LOG_TAG, presign);

				String post = "https://ws.audioscrobbler.com/2.0/?api_key="+API_KEY+"&api_sig="+presign+"&authToken="+token
						+"&method=auth.getMobileSession&username=alexdzeshko"
						+"&format=json";
				Log.d(LOG_TAG, post);
				new AsyncTask<String, Void, String>() {

					private String LOG_TAG = "AuthTask";

					@Override
					protected void onPreExecute() {
						mProgressBar.setVisibility(View.VISIBLE);
					}

					@Override
					protected String doInBackground(String... params) {
						try {
							/*ArrayList<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>();
							data.add(new BasicNameValuePair("method", "auth.getMobileSession"));
							data.add(new BasicNameValuePair("api_key", API_KEY));
							data.add(new BasicNameValuePair("api_sig", "8703cdfe1476560093daf1d0da890592"));
							data.add(new BasicNameValuePair("authToken", ""));
							data.add(new BasicNameValuePair("password", "hujykoli"));
							data.add(new BasicNameValuePair("username", "alexdzeshko"));
							data.add(new BasicNameValuePair("format", "json"));*/
							return HttpManager.getInstance().postRequest(params[0]);
						} catch (ClientProtocolException e) {
							Toast.makeText(AuthActivity.this, e.toString(),
									Toast.LENGTH_SHORT).show();
						} catch (IOException e) {
							Toast.makeText(AuthActivity.this, e.toString(),
									Toast.LENGTH_SHORT).show();
						}
						return null;
					}

					@Override
					protected void onPostExecute(String result) {
						Log.d(LOG_TAG, result);
						Toast.makeText(AuthActivity.this, result,
								Toast.LENGTH_LONG).show();
						Intent intent = new Intent(AuthActivity.this,
								MainActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
					}

				}.execute(post);
			}
		});
	}

}
